package com.youyi.yyfilepickerlibrary.core.loader;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.webkit.MimeTypeMap;
import com.lmiot.xyclick.Util.SpFlag;
import com.youyi.yyfilepickerlibrary.core.model.EssFile;
import com.youyi.yyfilepickerlibrary.core.util.Const;
import java.util.ArrayList;
import java.util.List;

public class EssMimeTypeLoader extends CursorLoader {
    private static final String[] PROJECTION = {"_id", "_data", "mime_type", "_size", "date_added", SpFlag.k, "date_modified"};
    private static final Uri QUERY_URI = MediaStore.Files.getContentUri("external");
    private List<EssFile> essFileList;

    public EssMimeTypeLoader(Context context, String str, String[] strArr, String str2) {
        super(context, QUERY_URI, PROJECTION, str, strArr, str2);
    }

    public static CursorLoader newInstance(Context context, String str, int i) {
        String[] strArr;
        String str2 = "mime_type='" + MimeTypeMap.getSingleton().getMimeTypeFromExtension(str) + "'";
        if (str == null) {
            str = "";
        }
        String str3 = "mime_type in(?,?) ";
        if (str.equalsIgnoreCase("doc") || str.equalsIgnoreCase("docx")) {
            strArr = new String[]{Const.mimeTypeMap.get("doc"), Const.mimeTypeMap.get("docx")};
            str2 = str3;
        } else {
            strArr = null;
        }
        if (str.equalsIgnoreCase("xls") || str.equalsIgnoreCase("xlsx")) {
            strArr = new String[]{Const.mimeTypeMap.get("xls"), Const.mimeTypeMap.get("xlsx")};
            str2 = str3;
        }
        if (str.equalsIgnoreCase("ppt") || str.equalsIgnoreCase("pptx")) {
            strArr = new String[]{Const.mimeTypeMap.get("ppt"), Const.mimeTypeMap.get("pptx")};
        } else {
            str3 = str2;
        }
        if (str.equalsIgnoreCase("png") || str.equalsIgnoreCase("jpg") || str.equalsIgnoreCase("jpeg")) {
            strArr = new String[]{Const.mimeTypeMap.get("png"), Const.mimeTypeMap.get("jpg"), Const.mimeTypeMap.get("jpeg")};
            str3 = "mime_type in(?,?,?)  and media_type != 0";
        }
        if (str.equalsIgnoreCase("apk")) {
            str3 = "_data LIKE '%.apk' ";
        }
        String str4 = str3 + " and " + "_size" + " >0 ";
        String str5 = "date_added DESC ";
        if (i == 0) {
            str5 = "_data ASC ";
        } else if (i == 1) {
            str5 = "_data DESC ";
        } else if (i == 2) {
            str5 = "date_added ASC ";
        } else if (i != 3) {
            if (i == 4) {
                str5 = "_size ASC ";
            } else if (i == 5) {
                str5 = "_size DESC ";
            }
        }
        return new EssMimeTypeLoader(context, str4, strArr, str5);
    }

    public List<EssFile> getEssFileList() {
        return this.essFileList;
    }

    @Override // android.support.v4.content.AsyncTaskLoader, android.support.v4.content.CursorLoader, android.support.v4.content.CursorLoader
    public Cursor loadInBackground() {
        Cursor loadInBackground = super.loadInBackground();
        this.essFileList = new ArrayList();
        if (loadInBackground != null) {
            while (loadInBackground.moveToNext()) {
                EssFile essFile = new EssFile(loadInBackground.getString(loadInBackground.getColumnIndexOrThrow("_data")));
                if (essFile.isExits()) {
                    this.essFileList.add(essFile);
                }
            }
            loadInBackground.moveToFirst();
        }
        return loadInBackground;
    }
}
